﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

namespace BesAsm.Emgaats.Maintenance
{
    public class UICheckLinksButton : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        private ApplicationWindow _appWindow;

        public UICheckLinksButton()
        {
            _appWindow = new ApplicationWindow(ArcMap.Application.hWnd);
        }

        protected override void OnClick()
        {
            System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;
            try
            {
                int totalErrors, totalWarnings;
                EmgaatsMaintenanceExtension.Extension.CheckEditLayers(out totalErrors, out totalWarnings);

                string message = string.Format("There were {1} errors and {2} warnings found.{0} See attribute tables for details.", Environment.NewLine, totalErrors, totalWarnings);
                MessageBoxIcon icon = MessageBoxIcon.Information;
                if (totalErrors > 0)
                {
                    icon = MessageBoxIcon.Error;
                }
                else if (totalWarnings > 0)
                {
                    icon = MessageBoxIcon.Warning;
                }

                MessageBox.Show(_appWindow, message, "Results", MessageBoxButtons.OK, icon);
            }
            catch (Exception ex)
            {
                string msg = string.Format("An error occured: {0}", ex.Message);
                MessageBox.Show(_appWindow, msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                System.Windows.Forms.Cursor.Current = Cursors.Default;
            }
        }

        protected override void OnUpdate()
        {
            bool enabled = false;
            if (ArcMap.Application != null &&
                EmgaatsMaintenanceExtension.Extension != null &&
                EmgaatsMaintenanceExtension.Extension.Settings != null &&
                EmgaatsMaintenanceExtension.Extension.Settings.AppendClass != null &&
                EmgaatsMaintenanceExtension.Extension.Settings.ArchiveClass != null)
            {
                enabled = true;
            }

            this.Enabled = enabled;
        }
    }
}
